// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <functional>
#include <memory>
#include <utility>
#include <vector>

#include <ktest/ktest.h>

#include <nebula/core/chunked_array.h>
#include <nebula/compute/api_scalar.h>
#include <tests/compute/kernels/test_util.h>
#include <nebula/testing/ktest_util.h>
#include <turbo/base/checked_cast.h>

namespace nebula {


namespace compute {

void CheckBooleanScalarArrayBinary(std::string func_name, Datum array) {
  for (std::shared_ptr<Scalar> scalar :
       {std::make_shared<BooleanScalar>(), std::make_shared<BooleanScalar>(true),
        std::make_shared<BooleanScalar>(false)}) {
    ASSERT_OK_AND_ASSIGN(auto constant_array,
                         MakeArrayFromScalar(*scalar, array.length()));

    ASSERT_OK_AND_ASSIGN(Datum expected,
                         call_function(func_name, {Datum(constant_array), array}));
    check_scalar(func_name, {scalar, array}, expected);

    ASSERT_OK_AND_ASSIGN(expected,
                         call_function(func_name, {array, Datum(constant_array)}));
    check_scalar(func_name, {array, scalar}, expected);
  }
}

TEST(TestBooleanKernel, Invert) {
  auto arr =
      assert_array_from_json(boolean(), "[true, false, true, null, false, true, false, null]");
  auto expected =
      assert_array_from_json(boolean(), "[false, true, false, null, true, false, true, null]");
  CheckScalarUnary("invert", arr, expected);
}

TEST(TestBooleanKernel, And) {
  auto left = assert_array_from_json(boolean(), "    [true, true,  true, false, false, null]");
  auto right = assert_array_from_json(boolean(), "   [true, false, null, false, null,  null]");
  auto expected = assert_array_from_json(boolean(), "[true, false, null, false, null,  null]");
  check_scalar_binary("and", left, right, expected);
  CheckBooleanScalarArrayBinary("and", left);
}

TEST(TestBooleanKernel, Or) {
  auto left = assert_array_from_json(boolean(), "    [true, true,  true, false, false, null]");
  auto right = assert_array_from_json(boolean(), "   [true, false, null, false, null,  null]");
  auto expected = assert_array_from_json(boolean(), "[true, true,  null, false, null,  null]");
  check_scalar_binary("or", left, right, expected);
  CheckBooleanScalarArrayBinary("or", left);
}

TEST(TestBooleanKernel, Xor) {
  auto left = assert_array_from_json(boolean(), "    [true,  true,  true, false, false, null]");
  auto right = assert_array_from_json(boolean(), "   [true,  false, null, false, null,  null]");
  auto expected = assert_array_from_json(boolean(), "[false, true,  null, false, null,  null]");
  check_scalar_binary("xor", left, right, expected);
  CheckBooleanScalarArrayBinary("xor", left);
}

TEST(TestBooleanKernel, AndNot) {
  auto left = assert_array_from_json(
      boolean(), "[true,  true,  true, false, false, false, null, null,  null]");
  auto right = assert_array_from_json(
      boolean(), "[true,  false, null, true,  false, null,  true, false, null]");
  auto expected = assert_array_from_json(
      boolean(), "[false, true,  null, false, false, null,  null, null,  null]");
  check_scalar_binary("and_not", left, right, expected);
  CheckBooleanScalarArrayBinary("and_not", left);
}

TEST(TestBooleanKernel, KleeneAnd) {
  auto left = assert_array_from_json(boolean(), "    [true, true,  true, false, false, null]");
  auto right = assert_array_from_json(boolean(), "   [true, false, null, false, null,  null]");
  auto expected = assert_array_from_json(boolean(), "[true, false, null, false, false, null]");
  check_scalar_binary("and_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("and_kleene", left);

  left = assert_array_from_json(boolean(), "    [true, true,  false, null, null]");
  right = assert_array_from_json(boolean(), "   [true, false, false, true, false]");
  expected = assert_array_from_json(boolean(), "[true, false, false, null, false]");
  check_scalar_binary("and_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("and_kleene", left);

  left = assert_array_from_json(boolean(), "    [true, true,  false, true]");
  right = assert_array_from_json(boolean(), "   [true, false, false, false]");
  expected = assert_array_from_json(boolean(), "[true, false, false, false]");
  check_scalar_binary("and_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("and_kleene", left);
}

TEST(TestBooleanKernel, KleeneAndNot) {
  auto left = assert_array_from_json(
      boolean(), "[true,  true,  true, false, false, false, null, null,  null]");
  auto right = assert_array_from_json(
      boolean(), "[true,  false, null, true,  false, null,  true, false, null]");
  auto expected = assert_array_from_json(
      boolean(), "[false, true,  null, false, false, false, false, null, null]");
  check_scalar_binary("and_not_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("and_not_kleene", left);

  left = assert_array_from_json(boolean(), "    [true,  true,  false, false]");
  right = assert_array_from_json(boolean(), "   [true,  false, true,  false]");
  expected = assert_array_from_json(boolean(), "[false, true,  false, false]");
  check_scalar_binary("and_not_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("and_not_kleene", left);
}

TEST(TestBooleanKernel, KleeneOr) {
  auto left = assert_array_from_json(boolean(), "    [true, true,  true, false, false, null]");
  auto right = assert_array_from_json(boolean(), "   [true, false, null, false, null,  null]");
  auto expected = assert_array_from_json(boolean(), "[true, true,  true, false, null,  null]");
  check_scalar_binary("or_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("or_kleene", left);

  left = assert_array_from_json(boolean(), "    [true, true,  false, null, null]");
  right = assert_array_from_json(boolean(), "   [true, false, false, true, false]");
  expected = assert_array_from_json(boolean(), "[true, true,  false, true, null]");
  check_scalar_binary("or_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("or_kleene", left);

  left = assert_array_from_json(boolean(), "    [true, true,  false, false]");
  right = assert_array_from_json(boolean(), "   [true, false, false, true]");
  expected = assert_array_from_json(boolean(), "[true, true,  false, true]");
  check_scalar_binary("or_kleene", left, right, expected);
  CheckBooleanScalarArrayBinary("or_kleene", left);
}

}  // namespace compute
}  // namespace nebula
